// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   StaffServiceImpl.java

package com.hfstu.pms.dataaccess.service.impl;

import com.google.common.collect.Lists;
import com.hfstu.framework.core.orm.Page;
import com.hfstu.framework.core.orm.PageRequest;
import com.hfstu.framework.util.StringUtil;
import com.hfstu.framework.uuid.UUIDGenerator;
import com.hfstu.pms.dataaccess.dao.IStaffDao;
import com.hfstu.pms.dataaccess.dao.IStaffDepartmentDao;
import com.hfstu.pms.dataaccess.service.IStaffService;
import com.hfstu.pms.dto.StaffDTO;
import com.hfstu.pms.model.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import org.apache.commons.beanutils.BeanUtils;

public class StaffServiceImpl
	implements IStaffService
{

	private IStaffDao staffDao;
	private IStaffDepartmentDao staffDepartmentDao;

	public StaffServiceImpl()
	{
		staffDao = null;
		staffDepartmentDao = null;
	}

	public Staff save(StaffDTO staffDTO)
	{
		Staff staff = new Staff();
		try
		{
			BeanUtils.copyProperties(staff, staffDTO);
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}
		if (staffDTO.getDeptIds() != null && staffDTO.getDeptIds().size() != 0)
		{
			List deptIds = staffDTO.getDeptIds();
			for (Iterator iterator = deptIds.iterator(); iterator.hasNext();)
			{
				String deptId = (String)iterator.next();
				if (!StringUtil.isNullOrEmpty(deptId))
				{
					StaffDepartment sd = new StaffDepartment();
					sd.setId(UUIDGenerator.getUUID());
					sd.setCreateTime(new Date());
					sd.setLastChangeTime(new Date());
					Department dept = new Department();
					dept.setId(deptId);
					sd.setStaff(staff);
					sd.setDepartment(dept);
					staff.getStaffDepartment().add(sd);
				}
			}

		}
		List sds = staff.getStaffDepartment();
		Staff s = (Staff)staffDao.save(staff);
		StaffDepartment staffDepartment;
		for (Iterator iterator1 = sds.iterator(); iterator1.hasNext(); staffDepartmentDao.save(staffDepartment))
			staffDepartment = (StaffDepartment)iterator1.next();

		return s;
	}

	public Staff update(StaffDTO staffDTO)
	{
		Staff staff = (Staff)staffDao.getById(staffDTO.getId());
		try
		{
			BeanUtils.copyProperties(staff, staffDTO);
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}
		List sds = staff.getStaffDepartment();
		if (sds != null)
		{
			StaffDepartment staffDepartment;
			for (Iterator iterator = sds.iterator(); iterator.hasNext(); staffDepartmentDao.delete(staffDepartment))
				staffDepartment = (StaffDepartment)iterator.next();

		}
		List newSds = Lists.newArrayList();
		if (staffDTO.getDeptIds() != null && staffDTO.getDeptIds().size() != 0)
		{
			List deptIds = staffDTO.getDeptIds();
			for (Iterator iterator1 = deptIds.iterator(); iterator1.hasNext();)
			{
				String deptId = (String)iterator1.next();
				if (!StringUtil.isNullOrEmpty(deptId))
				{
					StaffDepartment sd = new StaffDepartment();
					sd.setId(UUIDGenerator.getUUID());
					sd.setCreateTime(new Date());
					sd.setLastChangeTime(new Date());
					Department dept = new Department();
					dept.setId(deptId);
					sd.setStaff(staff);
					sd.setDepartment(dept);
					newSds.add(sd);
				}
			}

		}
		Staff s = (Staff)staffDao.update(staff);
		StaffDepartment staffDepartment;
		for (Iterator iterator2 = newSds.iterator(); iterator2.hasNext(); staffDepartmentDao.save(staffDepartment))
			staffDepartment = (StaffDepartment)iterator2.next();

		return s;
	}

	public Staff saveOrUpdate(Staff entity)
	{
		return (Staff)staffDao.saveOrUpdate(entity);
	}

	public void delete(String id)
	{
		staffDao.delete(id);
	}

	public void delete(Staff entity)
	{
		staffDao.delete(entity);
	}

	public Staff getById(String id)
	{
		return (Staff)staffDao.getById(id);
	}

	public List getByIds(String ids[])
	{
		return staffDao.getByIds(ids);
	}

	public List findAll()
	{
		return staffDao.findAll();
	}

	public List findListByCondition(Map filterPropertyMap, Map sortValueMap)
	{
		return staffDao.findListByCondition(filterPropertyMap, sortValueMap);
	}

	public Page findPageByCondition(PageRequest pageRequest, Map filterPropertyMap, Map sortValueMap)
	{
		return staffDao.findPageByCondition(pageRequest, filterPropertyMap, sortValueMap);
	}

	public IStaffDepartmentDao getStaffDepartmentDao()
	{
		return staffDepartmentDao;
	}

	public void setStaffDepartmentDao(IStaffDepartmentDao staffDepartmentDao)
	{
		this.staffDepartmentDao = staffDepartmentDao;
	}

	public IStaffDao getStaffDao()
	{
		return staffDao;
	}

	public void setStaffDao(IStaffDao staffDao)
	{
		this.staffDao = staffDao;
	}
}
